package com.shopnight.utils;

import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;


@Component
public class UserContext implements ApplicationContextAware {

    private static ApplicationContext applicationContext;


    @Override
    public void setApplicationContext(ApplicationContext context) throws BeansException {
        applicationContext = context;
    }

    private static final ThreadLocal<UserInfo> userThreadLocal = new ThreadLocal<>();

    /**
     * 保存当前登录用户信息到ThreadLocal
     *
     * @param userInfo 用户信息对象
     */
    public static void setUserInfo(UserInfo userInfo) {
        userThreadLocal.set(userInfo);
    }

    /**
     * 获取当前登录用户信息
     *
     * @return 用户信息对象
     */
    public static UserInfo getUserInfo() {
        return userThreadLocal.get();
    }


   public static String getEndUserId() {
       UserInfo userInfo = getUserInfo();

       return userInfo.getUserId();
    }


    /**
     * 移除当前登录用户信息
     */
    public static void removeUserInfo() {
        userThreadLocal.remove();
    }



}



